//
//  TGMapController.m
//  GroupBuy
//
//  Created by wwj on 15/11/2.
//  Copyright © 2015年 wwj. All rights reserved.
//


#import "TGMapController.h"
#import "TGHttpRequest.h"
#import "TGAnnotationModel.h"
#import "TGDealModel.h"
#import "TGBusinessModel.h"
#import "TGCustomAnnotationView.h"
#import <MAMapKit/MAMapKit.h>

@interface TGMapController ()<MAMapViewDelegate>
@property (strong, nonatomic) MAMapView *mapView;
@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation TGMapController

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地图";
    
    [self initMapView];
    //添加回到用户位置的按钮
    [self addBackView];
}

-(void)initMapView{
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    mapView.delegate = self;
    mapView.showsUserLocation = YES;
    mapView.showsCompass =NO;
    mapView.showsScale = NO;
    [self.view addSubview:mapView];
}

#pragma mark 添加回到用户位置的按钮
-(void)addBackView{
    UIButton *backUser = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"btn_map_locate.png"];
    [backUser setBackgroundImage:image forState:UIControlStateNormal];
    [backUser setBackgroundImage:[UIImage imageNamed:@"btn_map_locate_hl.png"] forState:UIControlStateNormal];
    CGFloat w = image.size.width;
    CGFloat h = image.size.height;
    CGFloat margin = 20;
    CGFloat x = self.view.frame.size.width - w - margin;
    CGFloat y = self.view.frame.size.height - h - margin;
    backUser.frame = CGRectMake(x, y, w, h);
    backUser.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
    [backUser addTarget:self action:@selector(backUserClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backUser];
}

#pragma mark 定位到当前位置
- (void)backUserClick{
    MACoordinateRegion region = MACoordinateRegionMake(_mapView.userLocation.coordinate, MACoordinateSpanMake(0.018404, 0.031468));
    [_mapView setRegion:region animated:YES];
}


#pragma -mark 高德地图代理方法
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation{
    
    if (_mapView) {
        return;
    }
    
    _mapView = mapView;
    [self backUserClick];
}

-(void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    [TGHttpRequest getgroupListWithPosition:mapView.region.center success:^(id data, id totalCount) {
        for (TGDealModel *model in data) {
            if ([self.dataArray containsObject:model]) {
                
                continue;
            }else{
                [self.dataArray addObject:model];
            }
            
            for (TGBusinessModel *bModel in model.businesses) {
                TGAnnotationModel *anModel = [[TGAnnotationModel alloc] init];
                anModel.business = bModel;
                anModel.deal= model;
                anModel.coordinate = CLLocationCoordinate2DMake(bModel.latitude, bModel.longitude);
                [mapView addAnnotation:anModel];
            }
        }
    } error:nil];
}

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(TGAnnotationModel *)annotation{
    if ([annotation isKindOfClass:[TGAnnotationModel class]]) {
        TGCustomAnnotationView *annotationView = [TGCustomAnnotationView customAnnotationViewWithMapView:mapView viewForAnnotation:annotation];
        return annotationView;
    }
    return nil;
}

-(void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view{

}

@end
